package cn.tedu.collection;

import java.util.*;

/*本类用于测试Collection相关API*/
/**
 * 知识点：
 *
 *
 * */
public class TestCollection {
    public static void main(String[] args) {
        Collection<Integer> c =new ArrayList<Integer>();//List 有序有下标可重复
        /*1.Integer是泛型，用来约束集合c中可以存入的元素类型
        * 泛型中的Type只能是引用类型，不能是基本类型*/

        c.add(100);
        c.add(200);
        c.add(300);
        c.add(400);
        c.add(500);
        System.out.println("c = " + c);//c = [100, 200, 300, 400, 500]

        //method
//        c.clear();
//        System.out.println("c = " + c);

        System.out.println("c.contains(300) = "+c.contains(300));//判断集合是否包含指定元素300,t
        System.out.println("c.hashCode() = " + c.hashCode());//127240651
        System.out.println("c.isEmpty() = " + c.isEmpty());//f
        System.out.println("c.remove(200) = " + c.remove(200));//t,移除集合中首次出现的指定元素
        System.out.println("c = " + c);//c = [100, 300, 400, 500]
        System.out.println("c.size() = " + c.size());//4
        System.out.println("c.equals(200) = " + c.equals(200));//f,

        Object[] array = c.toArray();//Alt+Enter
        System.out.println("Arrays.toString(array) = " + Arrays.toString(array));

        //
        Collection<Integer> c2=new ArrayList<Integer>();
        c2.add(2);
        c2.add(4);
        c2.add(6);
        System.out.println("c2 = " + c2);

        c.addAll(c2);
        System.out.println("c = " + c);
        System.out.println("c.contains(c2) = "+c.contains(c2));//f因为c集合中没有一个c2元素
        System.out.println("c.containsAll(c2) = "+c.containsAll(c2));//t，判断c是否包含c2中所有元素
        System.out.println("c.retainAll(c2) = " + c.retainAll(c2));//取两个集合之间的公共部分，交集
        System.out.println("c = " + c);//[2,4,6]

        System.out.println("c.removeAll(c2) = " + c.removeAll(c2));//差集
        System.out.println("c = " + c);
//        c.add(c2);Required type:Integer
        //遍历/迭代 元素
        /*1.获取集合对应的迭代器 Iterator<E> it= c.iterator()
        * 2.判断集合中是否有下个元素 it.hasNext()
        * 3.获取当前迭代到的元素 it.next()
        * 务必记住三个方法*/
        Iterator<Integer> it=c2.iterator();
        while (it.hasNext()){//判断
            //获取
            Integer next = it.next();
            System.out.println(next);
        }


    }
}
